#!/bin/bash
set -e
set -x
MYDIR="${0%/*}"
cd "$MYDIR/.."
source "recipes/common.sh"


# RECIPE START util-linux-native
# depends: ncurses
# XXX: util-linux supports building either all fdisks (fdisk, cfdisk, sfdisk)
# or none of them.
# XXX: ncurses builds both target and native libraries and tools
# (the latter are required for making terminfo files)

save_CFLAGS="$CFLAGS"
save_CPPFLAGS="$CPPFLAGS"
save_LDFLAGS="$LDFLAGS"
export CFLAGS='-O2 -g'
export CPPFLAGS="-I${xtools}${build_prefix}/include"
export LDFLAGS="-L${xtools}${build_prefix}/lib"


rm -rf build/util-linux-native
mkdir -p build/util-linux-native
cd build/util-linux-native
env \
	NCURSESW_LIBS="-L${xtools}${build_prefix}/lib -lncursesw -ltinfo" \
	NCURSESW_CFLAGS="-I${xtools}${build_prefix}/include" \
../../util-linux/configure \
	$NATIVE_CONFIGURE_ARGS \
	--bindir="$build_prefix/bin" \
	--disable-shared \
	--enable-static \
	--without-python \
	--without-readline \
	--without-tinfo \
	--without-ncurses \
	--disable-all-programs \
	--disable-bash-completion \
	--enable-libblkid \
	--enable-libfdisk \
	--enable-libsmartcols \
	--enable-libuuid \
	--enable-blkid \
	--enable-hardlink \
	--enable-fdisks \
	--enable-wipefs \
	--enable-uuidgen \
	--disable-makeinstall-chown \
	--disable-makeinstall-setuid \
	${AUTOCRAP_KLUDGE}

make -j${JOBS} V=1
make -j${JOBS} install DESTDIR=`pwd`/inst
mkdir -p "${xtools}${build_prefix}"
# fail if any required binares are missing
set -o pipefail
tar -c -f - -C inst${build_prefix} \
	bin/blkid \
	bin/hardlink \
	bin/sfdisk \
	bin/wipefs \
	bin/uuidgen \
	| \
tar -x -v -f - -C "${xtools}${build_prefix}"

cd ../..
rm -rf build/util-linux-native

export CFLAGS="$save_CFLAGS"
export CPPFLAGS="$save_CPPFLAGS"
export LDFLAGS="$save_LDFLAGS"

# RECIPE END util-linux-native
